﻿//////////////////////////////////////////////////////////////////
//
// ZeroMQ\Transport.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace ZeroMQ
{
   public class Transport : ITransport
   {

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport events
      /// </summary>
      public event NetworkStateChangedHandler EventStateChanged;
      public event ReceivedMessageHandler EventReceivedMessage;

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Private properties
      /// </summary>
      private static Logger logger_;
      private IAccount account_;
      private string senderId_;

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      static Transport()
      {
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public Transport()
      {
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool Initialize(string senderId)
      {
         try
         {
            // DON'T DO THIS IN THE CONSTRUCTOR, IT HASN'T BEEN INITIALIZED UNTIL NOW
            logger_ = Program.Logger;

            logger_.DebugFormat("initializing ZeroMQ");
            senderId_ = senderId;

            return true;
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public void Shutdown()
      {
         try
         {
            senderId_ = null;
            logger_.WarnFormat("ZeroMQ has shut down");
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool Connect(IAccount account)
      {
         try
         {
            account_ = account;
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool Disconnect(IAccount account)
      {
         try
         {
            account_ = null;
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool LoadTile(string planetName, ILocation location)
      {
         ILoadTile message = Program.Loader.Create<ILoadTile>();
         if (null == message)
         {
            return false;
         }
         message.PlanetName = planetName;
         message.Location = location.Clone();

         return Post<ILoadTile>(message);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool TileImage(string planetName, ILocation location, short[,] heights)
      {
         ITileImage message = Program.Loader.Create<ITileImage>();
         if (null == message)
         {
            return false;
         }
         message.PlanetName = planetName;
         message.Location = location.Clone();
         message.Heights = heights;

         return Post<ITileImage>(message);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool Broadcast<T>(IMessage msg)
         where T : IMessage
      {
         return Post<T>(msg);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Send the message on a specific routing key and exchange
      /// </summary>
      private bool Post<T>(IMessage msg)
         where T : IMessage
      {
         try
         {
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return true;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool OpenPlanet(string planet)
      {
         try
         {
            logger_.DebugFormat("opening connection to planet '{0}'", planet);
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool ClosePlanet(string planet)
      {
         try
         {
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool StartPlanet(string planet)
      {
         try
         {
            logger_.DebugFormat("starting planet '{0}'", planet);
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of ITransport
      /// </summary>
      public bool StopPlanet(string planet)
      {
         try
         {
         }
         catch (System.Exception ex)
         {
            logger_.Exception(ex);
         }
         return false;
      }
   }
}
